#!/bin/sh
# Manages service startup for Grafpup Linux

# Determine the path to this application.
CURDIR="`pwd`"
APPDIR=`dirname "$0"`
cd "${APPDIR}"
APPDIR="`pwd`"
cd "${CURDIR}"

# Make sure we're root
[ ! "`whoami`" = "root" ] && exec gksu $0

# Get translations
ENVIRONMENT_LANG=`echo $LANG | sed "s/\(..\).*/\1/"` #detect environment language 
case $ENVIRONMENT_LANG in
	de)	export LANGUAGE="german" ;;
	en)	export LANGUAGE="english" ;;
	es)	export LANGUAGE="spanish" ;;
	fr)	export LANGUAGE="french" ;;
	it)	export LANGUAGE="italian" ;;
	nb)	export LANGUAGE="norwegian" ;; #translation is norwegian bokmål
	nl)	export LANGUAGE="dutch" ;;
	nn)	export LANGUAGE="norwegian" ;; #Use norwegian bokmål, since there is no norwegian nynorsk
	ru)	export LANGUAGE="russian" ;;
	*)	export LANGUAGE="english" ;; #english for contries without translation
esac
. $APPDIR/locale/english.po #always run to fill gaps in translation
[ -f "$APPDIR/locale/${LANGUAGE}.po" ] && . $APPDIR/locale/${LANGUAGE}.po 2> /dev/null


# build the service list
$APPDIR/functions -buildlist

# the initial info message
echo "Service Manager initiated." > $HOME/.config/tmp/infotext

export SERVICE_DIALOG="
<window title=\"$LOC_MAIN\" icon_name=\"network-server\" window-position=\"1\">
 <vbox>
  <hbox>
   <frame $LOC_AUTOSTART_FRAME>
    <hbox>
     <button>
      <input file stock=\"gtk-apply\"></input>
      <label>$LOC_YES</label>
      <action>$APPDIR/functions -bootstart \$SERVICE</action>
      <action>$APPDIR/functions -buildlist</action>
      <action type=\"refresh\">SERVICE</action>
      <action type=\"refresh\">INFOTEXT</action>
     </button>
     <button>
      <input file stock=\"gtk-cancel\"></input>
      <label>$LOC_NO</label>
      <action>$APPDIR/functions -bootstop \$SERVICE</action>
      <action>$APPDIR/functions -buildlist</action>
      <action type=\"refresh\">SERVICE</action>
      <action type=\"refresh\">INFOTEXT</action>
     </button>
    </hbox>
   </frame>
   <frame $LOC_CONTROLS>
    <hbox>
     <button>
      <input file stock=\"gtk-execute\"></input>
      <label>$LOC_START</label>
      <action>$APPDIR/functions -start \$SERVICE</action>
      <action>$APPDIR/functions -buildlist</action>
      <action type=\"refresh\">SERVICE</action>
      <action type=\"refresh\">INFOTEXT</action>
     </button>
     <button>
      <input file stock=\"gtk-stop\"></input>
      <label>$LOC_STOP</label>
      <action>$APPDIR/functions -stop \$SERVICE</action>
      <action>$APPDIR/functions -buildlist</action>
      <action type=\"refresh\">SERVICE</action>
      <action type=\"refresh\">INFOTEXT</action>
     </button>
     <button>
      <input file stock=\"gtk-refresh\"></input>
      <label>$LOC_RESTART</label>
      <action>$APPDIR/functions -restart \$SERVICE</action>
      <action>$APPDIR/functions -buildlist</action>
      <action type=\"refresh\">SERVICE</action>
      <action type=\"refresh\">INFOTEXT</action>
     </button>
    </hbox>
   </frame>
  </hbox>
  <tree rules-hint=\"true\" exported-column=\"1\">
   <label>$LOC_TREE_HEADER</label>
   <input icon_column=\"0\">cat $HOME/.config/tmp/servicelist</input>
   <variable>SERVICE</variable>
   <action signal=\"cursor_changed\">$APPDIR/functions -info \$SERVICE</action>
   <action signal=\"cursor_changed\" type=\"refresh\">INFOTEXT</action>
   <height>200</height><width>475</width>
  </tree>
  <frame $LOC_INFO>
   <text>
    <input>cat $HOME/.config/tmp/infotext</input>
    <variable>INFOTEXT</variable>
   </text>
  </frame>
  <hbox>
   <button help>
    <action>$APPDIR/functions -help &</action>
   </button>
   <button>
    <input file stock=\"gtk-refresh\"></input>
    <label>$LOC_REFRESH</label>
    <action>$APPDIR/functions -buildlist</action>
    <action type=\"refresh\">SERVICE</action>
   </button>
   <button>
    <input file stock=\"gtk-close\"></input>
    <label>$LOC_CLOSE</label>
    <action>EXIT:abort</action>
   </button>
  </hbox>
 </vbox>
</window>
"


gtkdialog3 --program SERVICE_DIALOG
DIR="`pwd`"
cd $HOME/.config/tmp
rm -R *
cd "$DIR"







